package com.timeshare.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.timeshare.common.Result;
import com.timeshare.dto.SkillRequest;
import com.timeshare.dto.SkillVO;
import com.timeshare.service.SkillService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * 技能控制器
 */
@Tag(name = "技能管理")
@RestController
@RequestMapping("/skill")
public class SkillController {
    
    private final SkillService skillService;
    
    public SkillController(SkillService skillService) {
        this.skillService = skillService;
    }
    
    @Operation(summary = "发布技能")
    @PostMapping("/publish")
    public Result<?> publishSkill(Authentication authentication, @Valid @RequestBody SkillRequest request) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            skillService.publishSkill(userId, request);
            return Result.success("发布成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "更新技能")
    @PutMapping("/update/{id}")
    public Result<?> updateSkill(Authentication authentication, 
                                @PathVariable Long id, 
                                @Valid @RequestBody SkillRequest request) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            skillService.updateSkill(userId, id, request);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "删除技能")
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteSkill(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            skillService.deleteSkill(userId, id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取技能列表")
    @GetMapping("/list")
    public Result<Page<SkillVO>> getSkillList(
            Authentication authentication,
            @RequestParam(required = false) Integer category,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Long userId = authentication != null ? (Long) authentication.getPrincipal() : null;
            Page<SkillVO> result = skillService.getSkillList(userId, category, keyword, page, size);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取技能详情")
    @GetMapping("/detail/{id}")
    public Result<SkillVO> getSkillDetail(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = authentication != null ? (Long) authentication.getPrincipal() : null;
            SkillVO skill = skillService.getSkillDetail(userId, id);
            return Result.success(skill);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "收藏/取消收藏技能")
    @PostMapping("/favorite/{id}")
    public Result<?> toggleFavorite(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            skillService.toggleFavorite(userId, id);
            return Result.success("操作成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取我发布的技能")
    @GetMapping("/my")
    public Result<Page<SkillVO>> getMySkills(
            Authentication authentication,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            Page<SkillVO> result = skillService.getMySkills(userId, page, size);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取我收藏的技能")
    @GetMapping("/favorites")
    public Result<Page<SkillVO>> getMyFavorites(
            Authentication authentication,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            Page<SkillVO> result = skillService.getMyFavorites(userId, page, size);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

